/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.utils.DecryptDruidSource;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.common.web.BaseController;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.InputStream;

/**
 * 升级sql Controller
 * @author ThinkGem
 * @version 2013-6-2
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/db")
public class BackupController extends BaseController {
    @RequestMapping("toBackup")
    public String toBackup(){
        return "modules/sys/backupDB";
    }
	@RequestMapping("backup")
	public String backup(){
        DecryptDruidSource druidSource = SpringContextHolder.getBean("dataSource");
        druidSource.backup();
        return "modules/sys/backupDB";
    }

    @RequestMapping("toRestore")
    public String toRestore(){
	    return "modules/sys/restoreDB";
    }

    @RequestMapping("restore")
    public String restore(MultipartFile mfile){
        try{
            CommonsMultipartFile cf = (CommonsMultipartFile)mfile;
            DiskFileItem fi = (DiskFileItem)cf.getFileItem();
            InputStream inputStream = fi.getInputStream();
            DecryptDruidSource druidSource = SpringContextHolder.getBean("dataSource");
            druidSource.restore(inputStream);
            logger.info("还原完成");
        }
        catch(Exception e){
            logger.error("解析marc数据出错", e);
        }
        return "modules/sys/restoreDB";
    }
}
